/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include "downloadModel.h"

#include <QStandardItemModel>

#include "functions.h"

/*FDownloadModel::FDownloadModel(int rows, int columns, QObject *parent)
    : QStandardItemModel(0, 10, parent)
{
	Q_UNUSED(rows);
	Q_UNUSED(columns);

	this->setHeaderData(CD_FILENAME, Qt::Horizontal, tr("Filename"));
	this->setHeaderData(CD_PROGRESS, Qt::Horizontal, tr("Progress"));
	this->setHeaderData(CD_SPEED, Qt::Horizontal, tr("Speed"));
	this->setHeaderData(CD_TIMELEFT, Qt::Horizontal, tr("Time Left"));
	this->setHeaderData(CD_SIZE, Qt::Horizontal, tr("Size"));
	this->setHeaderData(CD_LINK, Qt::Horizontal, tr("Link"));
	this->setHeaderData(CD_ELAPSED, Qt::Horizontal, tr("Elapsed"));
	this->setHeaderData(CD_HIDDEN_DLID, Qt::Horizontal, tr("Downloader"));
	this->setHeaderData(CD_HIDDEN_STATE, Qt::Horizontal, tr("State"));
	this->setHeaderData(CD_HIDDEN_ID, Qt::Horizontal, tr("Id"));
}*/

FDownloadModel::FDownloadModel(QObject *parent)
    : QStandardItemModel(0, 10, parent)
{
	this->setHeaderData(CD_FILENAME, Qt::Horizontal, tr("Filename"));
	this->setHeaderData(CD_PROGRESS, Qt::Horizontal, tr("Progress"));
	this->setHeaderData(CD_SPEED, Qt::Horizontal, tr("Speed"));
	this->setHeaderData(CD_TIMELEFT, Qt::Horizontal, tr("Time Left"));
	this->setHeaderData(CD_SIZE, Qt::Horizontal, tr("Size"));
	this->setHeaderData(CD_LINK, Qt::Horizontal, tr("Link"));
	this->setHeaderData(CD_ELAPSED, Qt::Horizontal, tr("Elapsed"));
	this->setHeaderData(CD_HIDDEN_DLID, Qt::Horizontal, tr("Downloader"));
	this->setHeaderData(CD_HIDDEN_STATE, Qt::Horizontal, tr("State"));
	this->setHeaderData(CD_HIDDEN_ID, Qt::Horizontal, tr("Id"));
}

FDownloadModel::~FDownloadModel()
{

}

QStandardItem * FDownloadModel::createHoster(const QString &hoster)
{
    	bool found = false;
	QStandardItem *tmpHoster = getHosterItemByName(hoster, found);
	if(found) return tmpHoster;
    
	QStandardItem *parentItem = this->invisibleRootItem();

	//hoster item erstellen
        //QList<QStandardItem *> listHoster;

	//name
	QStandardItem *itemHoster = new QStandardItem(hoster);
	itemHoster->setIcon(QPixmap(":/toolbar/container.svg"));

        //listHoster.append(itemHoster);
        //fortschritt
        /*QStandardItem *itemText = new QStandardItem("0");
        listHoster.append(itemText);
        itemText = new QStandardItem("-");
        listHoster.append(itemText);
        itemText = new QStandardItem("-");
        listHoster.append(itemText);
        itemText = new QStandardItem("");
        listHoster.append(itemText);
        itemText = new QStandardItem("");
        listHoster.append(itemText);
        itemText = new QStandardItem("");
        listHoster.append(itemText);
*/
        parentItem->appendRow(itemHoster);
//        parentItem->appendRow(listHoster);

	return itemHoster;
}

QStandardItem * FDownloadModel::createFolder(QStandardItem * hoster, const QString &folder)
{
	//folder item erstellen
	QList<QStandardItem *> listFolder;

	//name
	QStandardItem *itemFolder = new QStandardItem(folder);
	itemFolder->setIcon(QPixmap(":/toolbar/folder.svg"));
	listFolder.append(itemFolder);

	//fortschritt
	QStandardItem *itemText = new QStandardItem("0");
	listFolder.append(itemText);
	itemText = new QStandardItem("-");
	listFolder.append(itemText);
	itemText = new QStandardItem("-");
	listFolder.append(itemText);
	itemText = new QStandardItem("");
	listFolder.append(itemText);
	itemText = new QStandardItem("");
	listFolder.append(itemText);
	itemText = new QStandardItem("");
	listFolder.append(itemText);

	hoster->appendRow(listFolder);

	return itemFolder;
}

QPair<QStandardItem *, QStandardItem *> FDownloadModel::addFolder(const QString &hoster, const QString &folder)
{
	bool found = false;
	QStandardItem *parentItem = getHosterItemByName(hoster, found);

	if(found)
	{
		QStandardItem *folderItem = getFolderItem(parentItem, folder, found);
		if(found)
		{
			return QPair<QStandardItem *, QStandardItem *>(parentItem, folderItem);
		} else
		{
			return QPair<QStandardItem *, QStandardItem *>(parentItem, createFolder(parentItem, folder));
		}
	} else
	{
		QStandardItem *tmpHoster = createHoster(hoster);
		return QPair<QStandardItem *, QStandardItem *>(tmpHoster, createFolder(tmpHoster, folder));
	}
}

QStandardItem * FDownloadModel::getFolderItem(QStandardItem * hoster, const QString &folder, bool &found)
{
	if(hoster->hasChildren())
	{
		for(int i=0; i<hoster->rowCount(); i++)
		{
			if(hoster->child(i)->text() == folder)
			{
				found = true;
				return hoster->child(i);
			}
		}

		found = false;
		return hoster;
	} else
	{
		found = false;
		return hoster;
	}
}

void FDownloadModel::addDownload(const QString &hoster, const QString &folder, const QString filename, const QString &url, const long &id, const int &state, const uint downloaded, const uint size)
{
	QList<QStandardItem *> listText;

	QStandardItem *itemText = new QStandardItem(filename);
	listText.append(itemText);

	//progress auf 100 setzen wenn schon abgeschlossen
	if(state == xmlFileLoader::STATE_FINISHED)
		itemText = new QStandardItem("100");
	else
		itemText = new QStandardItem("0");

	listText.append(itemText);

	itemText = new QStandardItem("-");
	listText.append(itemText);
	itemText = new QStandardItem("-");
	listText.append(itemText);
	itemText = new QStandardItem(getSizeString(downloaded, size));
	listText.append(itemText);
	itemText = new QStandardItem(url);
	listText.append(itemText);
	itemText = new QStandardItem("");
	listText.append(itemText);
	itemText = new QStandardItem("-1");
	listText.append(itemText);
	itemText = new QStandardItem(QString("%1").arg(state));
	listText.append(itemText);
	itemText = new QStandardItem(QString("%1").arg(id));
	listText.append(itemText);

	addFolder(hoster, folder).second->appendRow(listText);
}

QStandardItem * FDownloadModel::getHosterItemByName(const QString &hoster, bool &found)
{
	QStandardItem *parentItem = this->invisibleRootItem();

	if(parentItem->hasChildren())
	{
		for(int i=0; i<parentItem->rowCount(); i++)
		{
			if(parentItem->child(i)->text() == hoster)
			{
				found = true;
				return parentItem->child(i);
			}
		}

		found = false;
		return parentItem;
	} else
	{
		found = false;
		return parentItem;
	}
}
